/**
 * made in china by xufeng
 * 2007-6-7 下午01:33:10
 * 
 */
package net.litxuf.Util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.yeqiangwei.util.StringHelper;

/**
 * @author XuFeng
 * 
 */
public class URLUtil {
	/***************************************************************************
	 * 获取url中的请求字符串 然后存入一个Hashmap中
	 * 
	 * @param request
	 * @return
	 */
	public static HashMap<String, List<String>> getURL(
			HttpServletRequest request) {

		HashMap<String, List<String>> hash = new HashMap<String, List<String>>();
		String valueGet = request.getQueryString();
		if (valueGet != null) {
			String[] valuesplit = valueGet.split("&");
			int valength = valuesplit.length;
			for (int i = 0; i < valength; i++) {
				String var[] = valuesplit[i].split("=");
				String key = var[0];
				String value = var[1];

				List<String> list = hash.get(key);
				if (list == null) {
					list = new ArrayList<String>();
					hash.put(key, list);
				}
				list.add(value);
			}
		}

		return hash;

	}

	/***************************************************************************
	 * 取出相应的请求变量的值
	 * 
	 * @param hash
	 * @param paraName
	 * @return
	 */

	public static String getFromHashMap(HashMap<String, List<String>> hash,
			String paraName) {

		List<String> singerlist = hash.get(paraName);
		if (singerlist == null)
			return null;
		return singerlist.get(0);

	}

	/**
	 * 根据ID生成歌曲的URL地址
	 * 
	 * @param id
	 * @return
	 */
	public static String getPlayPath(long id)

	{

		String s = String.valueOf(id);
		if (s.length() > 2) {
			s = s.substring(0, 2);
		}
		s = StringHelper.encodeString("md5of32", s);
		;
		s = s.substring(0, 2).toLowerCase();
		String s1 = "http://www.1ting.com/player/" + s + "/player_" + id
				+ ".html";

		return s1;
	}

	public static void WriteTotaticHTML(String oldPath, String newPath)
			throws IOException {
		URL url = new URL(oldPath);
		InputStream is = url.openStream();
		File file = new File(newPath);
		if (!file.exists()) {
			File file2 = file.getParentFile();
			file2.mkdirs();
			file.createNewFile();
		}
		FileOutputStream bos = new FileOutputStream(file, true);

		byte[] fileByte = new byte[2048];
		int read = 0;
		while ((read = is.read(fileByte)) >= 0) {
			bos.write(fileByte, 0, read);
		}
		is.close();
		bos.close();
	}

	/**
	 * 
	 * Made in 2007-9-7上午09:09:10
	 * 
	 * @param oldPath
	 * @param newPath
	 * @return
	 * 
	 */
	public static boolean WriteDemo(String oldPath, String newPath) {
		try {
			URL url = new URL(oldPath);
			InputStream is = url.openStream();
			File file = new File(newPath);
			if (!file.exists()) {
				File file2 = file.getParentFile();
				file2.mkdirs();
				file.createNewFile();
			}
			FileOutputStream bos = new FileOutputStream(file);

			byte[] fileByte = new byte[2048];
			int read = 0;
			while ((read = is.read(fileByte)) >= 0) {
				bos.write(fileByte, 0, read);
			}
			is.close();
			bos.close();
			return true;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public static void main(String[] args) {
		// System.out.print(getPlayPath(138331));

	}

}
